package com.surveilans.controller;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import com.surveilans.classes.Kabupaten;
import com.surveilans.classes.Penyakit;
import com.surveilans.classes.Provinsi;
import com.surveilans.classes.Unit;
import com.surveilans.model.*;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Yudi
 */
public class HomeController extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setAttribute("title", "Home");
        if (request.getParameter("action")==null) {
            UnitModel unit = new UnitModel();
            DataModel data = new DataModel();
            InfoModel info = new InfoModel();
            if(request.getSession().getAttribute("authenticated")!=null){
                int role = (Integer)request.getSession().getAttribute("role");
                if( role == 1){
                    response.sendRedirect("./admin");
                }else if(role == 2){
                    response.sendRedirect("./unit");
                }else{
                    request.setAttribute("title", "Home");
                    ArrayList<Provinsi> ap = unit.getProvinsi();
                    ArrayList<Kabupaten> ak = unit.getKabupaten();
                    ArrayList<Penyakit> alp = data.getPenyakit();

                    request.setAttribute("provinsi", ap);
                    request.setAttribute("kabupaten", ak);
                    request.setAttribute("penyakit", alp);
                    request.getRequestDispatcher("info/map.jsp").forward(request, response);
                }
            }else{
                request.setAttribute("title", "Home");
                ArrayList<Provinsi> ap = unit.getProvinsi();
                ArrayList<Kabupaten> ak = unit.getKabupaten();
                ArrayList<Penyakit> alp = data.getPenyakit();
                String path = getServletContext().getRealPath("indonesia/data.csv");
                info.createDataCSV(path);

                request.setAttribute("provinsi", ap);
                request.setAttribute("kabupaten", ak);
                request.setAttribute("penyakit", alp);
                request.getRequestDispatcher("info/map.jsp").forward(request, response);
            }
        }
        else if(request.getParameter("action").equals("login_admin")){
            //login admin
            if (request.getMethod().equals("POST")) {
                String username = request.getParameter("username");
                String password = request.getParameter("password");
                
                UserModel userModel = new UserModel();
                int userId = userModel.validate(username, password);
                
                if (userId > 0) {                    
                    request.getSession().setAttribute("authenticated", true);
                    request.getSession().setAttribute("role", 1);
                    request.getSession().setAttribute("user", username);
                    response.sendRedirect("./home");
                } else {
                    request.getSession().setAttribute("flash_type", "error");
                    request.getSession().setAttribute("flash", "username atau password yang anda gunakan salah");
                    request.setAttribute("title", "Login Admin");
                    request.getRequestDispatcher("login_admin.jsp").forward(request, response);
                }
            } else {
                request.setAttribute("title", "Login Admin");
                request.getRequestDispatcher("login_admin.jsp").forward(request, response);
            }
        }else if(request.getParameter("action").equals("login_unit")){
            //login unit
            if (request.getMethod().equals("POST")) {
                String username = request.getParameter("username");
                String password = request.getParameter("password");
                
                UnitModel unit = new UnitModel();
                int unitId = unit.validate(username, password);
                Unit u = unit.getUnit(unitId);
                
                if (unitId > 0) {                    
                    request.getSession().setAttribute("authenticated", true);
                    request.getSession().setAttribute("role", 2);
                    request.getSession().setAttribute("unit_id", unitId);
                    request.getSession().setAttribute("user", u.getNama());
                    response.sendRedirect("./home");
                } else {
                    request.getSession().setAttribute("flash_type", "error");
                    request.getSession().setAttribute("flash", "username atau password yang anda gunakan salah");
                    request.setAttribute("title", "Login Unit Surveilans");
                    request.getRequestDispatcher("login_unit.jsp").forward(request, response);
                }
            } else {
                request.setAttribute("title", "Login Unit Surveilans");
                    request.getRequestDispatcher("login_unit.jsp").forward(request, response);
            }
        }
        else if(request.getParameter("action").equals("logout")){
            //logout
            request.getSession().removeAttribute("authenticated");
            response.sendRedirect("./home");
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
